<!-- 中成检验台账 -->
<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item>
        <el-select placeholder="请选择分类" size="mini" v-model="queryParam.category">
          <el-option v-for="item in categorys" :key="item.value" :label="item.text" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="reportTimeRange" type="daterange" align="right" unlink-panels value-format="yyyy-MM-dd"
          range-separator="-" start-placeholder="报告起始时间" end-placeholder="报告结束时间" :picker-options="pickerOptions" :clearable="true" size="mini">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="registTimeRange" type="daterange" align="right" unlink-panels value-format="yyyy-MM-dd"
          range-separator="-" start-placeholder="请验起始时间" end-placeholder="请验结束时间" :picker-options="pickerOptions" :clearable="true" size="mini">
        </el-date-picker>
      </el-form-item>

			<el-form-item>
			  <el-select v-model="queryParam.status" clearable placeholder="全部状态" size="mini">
			    <el-option label="待处理" value="0" />
			    <el-option label="待取样" value="1" />
			    <el-option label="待收样" value="2" />
			    <el-option label="待分发" value="3" />
			    <el-option label="检验中" value="4,5" />
			    <el-option label="待结论" value="6" />
			    <el-option label="待放行" value="7,10" />
			    <el-option label="已完成" value="8,11" />
			  </el-select>
			</el-form-item>

      <el-form-item>
        <el-select placeholder="请选择生产车间" v-model="queryParam.workshopId" :clearable="true" size="mini" filterable remote :remote-method="queryWorkshop">
          <el-option v-for="workshop in workshops" :key="workshop.workshopId" :label="workshop.workshopName" :value="workshop.workshopId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select placeholder="请选择品名" v-model="queryParam.productId" :clearable="true" size="mini" filterable remote :remote-method="queryProduct">
          <el-option v-for="product in products" :key="product.productId" :label="product.productName+'/'+product.productGmp" :value="product.productId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="批号" v-model="queryParam.batchNum" :clearable="true" size="mini"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="pan-btn primary-btn" icon="el-icon-search" size="mini" @click="queryData">搜索</el-button>
      </el-form-item>
	<el-form-item>
	  <el-button type="primary" class="pan-btn success-btn" icon="el-icon-download" size="mini" @click="exportData">导出</el-button>
	</el-form-item>
    </el-form>
    <el-table :row-style="{height: '10px'}" :cell-style="{padding:'0px 0'}" :data="tableData" border style="font-size: 1rem;" v-loading="loading">
      <template v-for="(title,index) in tableHead">
        <el-table-column show-overflow-tooltip :label="title" :key="index" width="150px">
			<template slot-scope="{row}">
				<span v-html="row[title]"></span>
			</template>
		</el-table-column>
      </template>
    </el-table>
    <el-pagination
      background
	  :page-size="20"
      layout="total, prev, pager, next"
      :total="totalPage"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
  import {
    zcCheckData,
    visit
  } from '@/api/tables'
  import {
    getProductList,
    getProductCategorys
  } from '@/api/product'
  import {
    getWorkshopList
  } from '@/api/workshop'

  export default {
    data() {
      return {
        loading:false,
        totalPage:0,
        reportTimeRange:"",
        registTimeRange:"",
        items: "",
        queryParam: {
          pageIndex:1,
          pageSize:20,
          category: 4,
          reportStartTime: "",
          reportEndTime: "",
          registStartTime: "",
          registEndTime: "",
          workshopId:"",
          productId:"",
          batchNum:"",
		  status:""
        },
        workshops:[],
        products:[],
        tableHead: [],
        choiceHead:[],
        tableData: [],
        categorys: [{
          text: "中间品",
          value: 4
        }, {
          text: "成品",
          value: 5
        }],
        pickerOptions: {
          shortcuts: [{
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', [new Date(), new Date()]);
            }
          }, {
            text: '今年至今',
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近六个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }
    },
    watch:{
      items: function(newData, oldData){
        this.tableHead = newData;
      }
    },
    mounted() {
	  visit("中成检验台账查询").then().catch()
      this.searchData()
    },
    methods: {
    queryData(){
      this.queryParam.pageIndex = 1;
      this.searchData()
    },
		exportData(){
			if(this.queryParam.productId == ''){
				this.$message.error("请先选择品名信息")
				return
			}
			window.open('/api/zcCheckTable/export/'+ this.queryParam.category +'/' + this.queryParam.productId, '_blank')
		},
      handleCurrentChange(val){
        this.queryParam.pageIndex = val;
        this.searchData()
      },
      searchData() {
        this.loading = true;
        if(this.reportTimeRange != null && this.reportTimeRange != ''){
          this.queryParam.reportStartTime = this.reportTimeRange[0]
          this.queryParam.reportEndTime = this.reportTimeRange[1]
        }
        if(this.registTimeRange != null && this.registTimeRange != ''){
          this.queryParam.registStartTime = this.registTimeRange[0]
          this.queryParam.registEndTime = this.registTimeRange[1]
        }
        zcCheckData(this.queryParam).then(res => {
            this.tableHead = res.tableHead
            this.tableData = res.tableData
            this.choiceHead = res.tableHead
            this.totalPage = res.total
          this.loading = false;
        })
      },
      queryWorkshop(query){
        let param = {
          page:1,
          limit:10,
          workshopName:query
        }
        getWorkshopList(param).then(res => {
          this.workshops = res.page.list
        })
      },
      queryProduct(query){
        let param = {
          page:1,
          limit:10,
          productName:query,
          productCategoryId:this.queryParam.category
        }
        getProductList(param).then(res => {
          this.products = res.page.list
        })
      }
    }
  }
</script>

<style>


</style>
